/**
 * @Description: 数据分析相关API
 * @Author: lujq
 * @Date: 2025-01-04
 * @LastEditors: lujq
 * @LastEditTime: 2025-01-04 10:56:00
 */

import { http } from './index'
import type { SalesData, ChartData } from '@/types'

/**
 * 获取销售趋势数据
 * @param params 查询参数
 * @returns 销售趋势数据
 */
export const getSalesTrend = (params: {
  startDate: string
  endDate: string
  type?: 'daily' | 'weekly' | 'monthly'
}) => {
  const queryParams = new URLSearchParams()
  queryParams.append('startDate', params.startDate)
  queryParams.append('endDate', params.endDate)
  if (params.type) queryParams.append('type', params.type)
  
  return http.get<SalesData[]>(`/sales-trend?${queryParams.toString()}`)
}

/**
 * 获取客户分布热力图数据
 * @returns 客户分布数据
 */
export const getCustomerHeatmap = () => {
  return http.get<ChartData[]>('/analytics/customer-heatmap')
}

/**
 * 获取布匹参数对比数据
 * @param productIds 产品ID列表
 * @returns 对比数据
 */
export const getFabricComparison = (productIds: string[]) => {
  return http.get(`/analytics/fabric-comparison?productIds=${productIds.join(',')}`)
}

/**
 * 获取销售排行榜
 * @param params 查询参数
 * @returns 排行榜数据
 */
export const getSalesRanking = (params: {
  type: 'product' | 'customer' | 'region'
  period: 'week' | 'month' | 'quarter' | 'year'
  limit?: number
}) => {
  const queryParams = new URLSearchParams()
  queryParams.append('type', params.type)
  queryParams.append('period', params.period)
  if (params.limit) queryParams.append('limit', params.limit.toString())
  
  return http.get<ChartData[]>(`/analytics/sales-ranking?${queryParams.toString()}`)
}

/**
 * 获取客户价值分析
 * @returns 客户价值数据
 */
export const getCustomerValue = () => {
  return http.get('/analytics/customer-value')
}

/**
 * 获取产品销售分析
 * @returns 产品销售数据
 */
export const getProductSales = () => {
  return http.get('/product-sales')
}

/**
 * 获取收入分析
 * @param params 查询参数
 * @returns 收入分析数据
 */
export const getRevenueAnalysis = (params: {
  startDate: string
  endDate: string
  groupBy?: 'day' | 'week' | 'month'
}) => {
  const queryParams = new URLSearchParams()
  queryParams.append('startDate', params.startDate)
  queryParams.append('endDate', params.endDate)
  if (params.groupBy) queryParams.append('groupBy', params.groupBy)
  
  return http.get(`/analytics/revenue?${queryParams.toString()}`)
}

/**
 * 获取客户流失预警
 * @returns 流失预警数据
 */
export const getChurnWarning = () => {
  return http.get('/analytics/churn-warning')
}

/**
 * 获取库存预警
 * @returns 库存预警数据
 */
export const getStockWarning = () => {
  return http.get('/analytics/stock-warning')
}

/**
 * 获取业务概览数据
 * @returns 业务概览数据
 */
export const getBusinessOverview = () => {
  return http.get('/overview')
}
